package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
@RequiredArgsConstructor// new EmpController() - 没有无参构造器
public class EmpController {

    private final EmpService empService;

    /**
     * 分页查询
     *
     * @param name     名称
     * @param gender   性别
     * @param begin    开始
     * @param end      终止
     * @param page     页
     * @param pageSize 页面大小
     * @return {@link Result}
     */
    @GetMapping
    public Result<PageBean<Emp>> page(String name,
                                      Short gender,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean<Emp> pageBean = empService.page(name, gender, begin, end, page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除员工
     *
     * @param ids 1,2,3,4,5
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids) {
        log.info("根据ids删除员工信息-{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 新增员工 - 所有接口都要加注释 interface
     *
     * @param emp emp
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    public Result<?> create(@RequestBody Emp emp) {
        empService.create(emp);
        return Result.success();
    }

    /**
     * 按id查询
     *
     * @param id id
     * @return {@link Result}<{@link Emp}>
     */
    @GetMapping("/{id}")
    public Result<Emp> queryById(@PathVariable Integer id) {
        Emp emp = empService.queryById(id);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     *
     * @param emp emp
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping
    public Result<?> update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }

}
